<?php

namespace app\common\model;

use common\model\base\BaseModel;
use common\traits\InstanceTrait;

class Category extends BaseModel
{
    use InstanceTrait;

    protected $insert = [
        'status' => 1,
    ];
    protected $pk = 'category_id';

    /**
     * 基础的where,所有查询都应以此为查询条件
     * @param array $param
     * @return mixed
     */
    public function getBaseWhere($param = [], $alias = '')
    {
        $where = parent::getBaseWhere($param, $alias);
        if (!empty($param['keywords'])) {
            $where[] = ['title', 'like', "%{$param['keywords']}%"];
        }
        if (!empty($param['admin_id'])) {
            $where[] = ['creater_id', '=', (string)($param['admin_id'])];
        }
        return $where;
    }

    /**
     * [createData 新建]
     * @linchuangbin
     * @DateTime  2017-02-10T21:19:06+0800
     * @param array $param [description]
     * @return bool [array]                         [description]
     * @throws Exception
     */
    public function createData($param)
    {
        try {
            $this->data($param)->allowField(true)->save();
            return true;
        } catch (Exception $e) {
            throw $e;
        }
    }

    /**
     * 获取所有针对书籍的评论。
     */
    public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable');
    }
}
